package com.blog.page;


/**
 * 共[1/8]页 第一页 上一页  1 2 3 4 5 下一页  最末页
 */
public class PagesModel3 {

    /**
     * 
     * @param cl
     * @param pageNum
     * @return
     */
    public String genPageHtml(CommonList cl, int pageNum) {
        StringBuffer buffer = new StringBuffer();
        buffer.append("共[" + cl.pageNo + "/" + cl.pageNum + "]页" + "&nbsp;");
        if (cl.pageNo == 1) {
            buffer.append("第一页" + "&nbsp;");
            buffer.append("上一页" + "&nbsp;");
        } else if (cl.pageNo > 1) {
            buffer.append(" <a href=\"JavaScript:tunePage(" + 1 + ",'','" + cl.searchStr + "')\">第一页</a>" + "&nbsp;");
            buffer.append(" <a href=\"JavaScript:tunePage(" + (cl.pageNo - 1) + ",'','" + cl.searchStr + "')\">上一页</a>" + "&nbsp;");
        }
        int currentNum = (cl.pageNo % pageNum == 0 ? (cl.pageNo / pageNum) - 1 : (int) (cl.pageNo / pageNum)) * pageNum;
        if (currentNum < 0) currentNum = 0;

        //---------------------------------
        // 显示中间可用的翻页码
        //---------------------------------
        for (int i = 0; i < pageNum; i++) {
            if ((currentNum + i + 1) > cl.pageNum || cl.pageNum < 2) break;
            buffer.append(" <a href=\"JavaScript:tunePage(" + (currentNum + i + 1) + ",'','" + cl.searchStr + "')\">");
            buffer.append(currentNum + i + 1 == cl.pageNo ? "<b>" + (currentNum + i + 1) + "</b>" : (currentNum + i + 1) + "");
            buffer.append("</a>");
        }

        if (cl.pageNo == cl.pageNum) {
            buffer.append("下一页" + "&nbsp;");
            buffer.append("最末页" + "&nbsp;");
        } else if (cl.pageNo < cl.pageNum) {
            buffer.append(" <a href=\"JavaScript:tunePage(" + (cl.pageNo + 1) + ",'','" + cl.searchStr + "')\">下一页</a>" + "&nbsp;");
            buffer.append(" <a href=\"JavaScript:tunePage(" + cl.pageNum + ",'','" + cl.searchStr + "')\">最末页</a>" + "&nbsp;");
        }
        return buffer.toString();
    }

}

